using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Common;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    partial class OptionsSplitsCriteriaSelectionDialog : Form
    {
        public OptionsSplitsCriteriaSelectionDialog(IActivity activity)
        {
            InitializeComponent();

            m_Activity = activity;

            this.Text = PluginView.GetLocalizedString("SelectSplitTypeText");

            RecordedLapsRadioButton.Text = PluginView.GetLocalizedString("RecordedLapsText");
            RecordedLapsRadioButton.Checked = true;

            CustomDistanceRadioButton.Text = PluginView.GetLocalizedString("CustomDistanceLapsText");

            CustomTimeRadioButton.Text = PluginView.GetLocalizedString("CustomTimeLapsText");

            DistanceUnitComboBox.Items.Add(Length.Units.Meter);
            DistanceUnitComboBox.Items.Add(Length.Units.Kilometer);
            DistanceUnitComboBox.Items.Add(Length.Units.Yard);
            DistanceUnitComboBox.Items.Add(Length.Units.Mile);
            DistanceUnitComboBox.SelectedItem = Utils.MajorLengthUnit(m_Activity != null ? m_Activity.Category.DistanceUnits : PluginMain.GetApplication().SystemPreferences.DistanceUnits);
            DistanceUnitComboBox.Format += new ListControlConvertEventHandler(DistanceUnitComboBox_Format);

            SplitDistanceLabel.Text = PluginView.GetLocalizedString("SplitDistanceLabelText");
            MaxDistanceLabel.Text = PluginView.GetLocalizedString("MaxDistanceLabelText");
            MaxDistanceUnitLabel.Text = Length.LabelAbbr((Length.Units)DistanceUnitComboBox.SelectedItem);
            StartAtDistanceLabel.Text = PluginView.GetLocalizedString("StartingAtText");
            StartAtUnitLabel.Text = Length.LabelAbbr((Length.Units)DistanceUnitComboBox.SelectedItem);

            SplitsTimeLabel.Text = PluginView.GetLocalizedString("SplitTimeLabelText");
            MaxSplitsTimeLabel.Text = PluginView.GetLocalizedString("MaxTimeLabelText");
            StartAtTimeLabel.Text = PluginView.GetLocalizedString("StartingAtText");

            LapDistanceTextBox.Text = ((float)1).ToString("0.0");
            MaxDistanceTextBox.Text = ((float)20).ToString("0.0");
            StartAtDistanceTextBox.Text = ((float)0).ToString("0.0");
        }

        void DistanceUnitComboBox_Format(object sender, ListControlConvertEventArgs e)
        {
            Length.Units item = (Length.Units)e.ListItem;

            e.Value = Length.LabelAbbr(item);
        }

#region UI callbacks

        private void OKButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void SplitTypeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            DistanceFlowLayoutPanel.Visible = CustomDistanceRadioButton.Checked;
            TimeFlowLayoutPanel.Visible = CustomTimeRadioButton.Checked;
        }

        private void DistanceTextBox_Validating(object sender, CancelEventArgs e)
        {
            float distanceEntered;

            if (!float.TryParse(LapDistanceTextBox.Text, out distanceEntered) || distanceEntered <= 0)
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidDistanceEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

        private void MaxDistanceTextBox_Validating(object sender, CancelEventArgs e)
        {
            float distanceEntered;

            if (!float.TryParse(MaxDistanceTextBox.Text, out distanceEntered) || distanceEntered <= 0)
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidDistanceEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

        private void StartAtDistanceTextBox_Validating(object sender, CancelEventArgs e)
        {
            float distanceEntered;

            if (!float.TryParse(StartAtDistanceTextBox.Text, out distanceEntered) || distanceEntered < 0)
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidDistanceEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

        private void DistanceUnitComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaxDistanceUnitLabel.Text = Length.LabelAbbr((Length.Units)DistanceUnitComboBox.SelectedItem);
            StartAtUnitLabel.Text = Length.LabelAbbr((Length.Units)DistanceUnitComboBox.SelectedItem);
        }

#endregion

        public IFilterCriteria GetSelectedCriteria()
        {
            if (RecordedLapsRadioButton.Checked)
            {
                return new TemplateRecordedLapsFilterCriteria(m_Activity);
            }
            else if (CustomDistanceRadioButton.Checked)
            {
                if (m_PreviousCriteria is TemplateCustomDistanceLapsFilterCriteria)
                {
                    TemplateCustomDistanceLapsFilterCriteria previousCriteria = m_PreviousCriteria as TemplateCustomDistanceLapsFilterCriteria;

                    if (float.Parse(LapDistanceTextBox.Text) == previousCriteria.LapDistance &&
                        float.Parse(MaxDistanceTextBox.Text) == previousCriteria.MaximumDistance &&
                        float.Parse(StartAtDistanceTextBox.Text) == previousCriteria.StartOffset &&
                        (Length.Units)DistanceUnitComboBox.SelectedItem == previousCriteria.DistanceUnits)
                    {
                        return previousCriteria;
                    }
                }

                TemplateCustomDistanceLapsFilterCriteria result = new TemplateCustomDistanceLapsFilterCriteria(float.Parse(MaxDistanceTextBox.Text),
                                                                                                               float.Parse(LapDistanceTextBox.Text),
                                                                                                               float.Parse(StartAtDistanceTextBox.Text),
                                                                                                               (Length.Units)DistanceUnitComboBox.SelectedItem);
                result.Activity = m_Activity;

                return result;
            }
            else
            {
                if (m_PreviousCriteria is TemplateCustomTimeLapsFilterCriteria)
                {
                    TemplateCustomTimeLapsFilterCriteria previousCriteria = m_PreviousCriteria as TemplateCustomTimeLapsFilterCriteria;

                    if (LapTimeDurationUpDown.Duration == (int)previousCriteria.LapTime.TotalSeconds &&
                        MaxTimeDurationUpDown.Duration == (int)previousCriteria.MaximumTime.TotalSeconds &&
                        StartAtTimeUpDown.Duration == (int)previousCriteria.StartOffset.TotalSeconds)
                    {
                        return previousCriteria;
                    }
                }

                TemplateCustomTimeLapsFilterCriteria result = new TemplateCustomTimeLapsFilterCriteria(new TimeSpan(0, 0, MaxTimeDurationUpDown.Duration),
                                                                                                       new TimeSpan(0, 0, LapTimeDurationUpDown.Duration),
                                                                                                       new TimeSpan(0, 0, StartAtTimeUpDown.Duration));
                result.Activity = m_Activity;

                return result;
            }
        }

        public IFilterCriteria PreviousCriteria
        {
            set
            {
                if (value is TemplateCustomDistanceLapsFilterCriteria)
                {
                    TemplateCustomDistanceLapsFilterCriteria criteria = value as TemplateCustomDistanceLapsFilterCriteria;

                    CustomDistanceRadioButton.Checked = true;
                    LapDistanceTextBox.Text = criteria.LapDistance.ToString("0.0");
                    MaxDistanceTextBox.Text = criteria.MaximumDistance.ToString("0.0");
                    DistanceUnitComboBox.SelectedItem = criteria.DistanceUnits;
                    StartAtDistanceTextBox.Text = criteria.StartOffset.ToString("0.0");
                }
                else if (value is TemplateCustomTimeLapsFilterCriteria)
                {
                    TemplateCustomTimeLapsFilterCriteria criteria = value as TemplateCustomTimeLapsFilterCriteria;

                    CustomTimeRadioButton.Checked = true;
                    LapTimeDurationUpDown.Duration = (UInt16)criteria.LapTime.TotalSeconds;
                    MaxTimeDurationUpDown.Duration = (UInt16)criteria.MaximumTime.TotalSeconds;
                    StartAtTimeUpDown.Duration = (UInt16)criteria.StartOffset.TotalSeconds;
                }

                m_PreviousCriteria = value;
            }
        }

        private IActivity m_Activity = null;
        private IFilterCriteria m_PreviousCriteria;

    }
}